package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.mine.SpecialBean;
import com.chzc.fm.duotin.utils.FileUtils;
import com.chzc.fm.duotin.utils.ImageUtil;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.utils.NumberUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.List;

/**
 * Created by BillSinla on 2016/7/6.
 */
public class MineRecyclerAdapter extends RecyclerView.Adapter<MineRecyclerAdapter.MineViewHolder>{

    private Context context;

    private List<SpecialBean> specialBeanList;

    private LayoutInflater inflater;

    public MineRecyclerAdapter(Context context, List<SpecialBean> specialBeanList) {
        this.context = context;
        this.specialBeanList = specialBeanList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.adapter_mine_item, null);
        return new MineViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MineViewHolder holder, int position) {
        holder.bindView(position);
    }

    @Override
    public int getItemCount() {
        return specialBeanList != null ? specialBeanList.size() : 0;
    }

    protected class MineViewHolder extends RecyclerView.ViewHolder{
        private TextView textTitle, textSpace, textNum;
        private ImageView imageAvatar;
        private View viewLine;
        public MineViewHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.tv_mine_title);
            textSpace = (TextView) itemView.findViewById(R.id.tv_mine_space);
            textNum = (TextView) itemView.findViewById(R.id.tv_mine_load_num);
            imageAvatar = (ImageView) itemView.findViewById(R.id.iv_mine_avatar);
            viewLine = itemView.findViewById(R.id.v_mine_line);
        }
        protected void bindView(int position){
            SpecialBean.AlbumBean albumBean = specialBeanList.get(position).getAlbum();
            List<SpecialBean.ContentListBean> contentList = specialBeanList.get(position).getContent_list();
            textTitle.setText(albumBean.getTitle());
            long space = 0;
            for (SpecialBean.ContentListBean content:contentList) {
                space += Long.parseLong(content.getAudio_32_size());
            }
            LogUtil.w("MineRecyclerAdapter: space:" + space);
            textSpace.setText("占用空间：" + NumberUtil.countSpace(space));
            textNum.setText("已下载：" + contentList.size());

//            File file = new File(FileUtils.DIR_IMAGE, FileUtils.getFileNameByHashCode(albumBean.getImage_url()));
//            if(file.exists()){
//                Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
//                imageAvatar.setImageBitmap(bitmap);
//            }

            ImageLoader.getInstance().displayImage(albumBean.getImage_url(), imageAvatar, ImageUtil.getRoundOptions());
            if(position == specialBeanList.size() - 1){
                viewLine.setVisibility(View.GONE);
            }
        }
    }
}
